# Created by L2Emu Team
import sys
from com.l2emu                               import L2DatabaseFactory
from com.l2emu.gameserver.datatables         import NpcTable
from com.l2emu.gameserver.datatables         import SpawnTable
from com.l2emu.gameserver.model              import L2Spawn
from com.l2emu.gameserver.model.quest        import State
from com.l2emu.gameserver.model.quest        import QuestState
from com.l2emu.gameserver.model.quest.jython import QuestJython as JQuest

qn = "1107_enter_hellbound_island"

WARPGATES = [32314,32315,32316,32317,32318,32319]

def checkEnter() :
  enterHellbound = False
  con = L2DatabaseFactory.getInstance().getConnection()
  offline = con.prepareStatement("SELECT enterHellbound FROM characters WHERE enterHellbound = 1")
  rs = offline.executeQuery()
  if rs :
    rs.next()
    try :
      enterHellbound = rs.getInt("enterHellbound")
      con.close()
    except :
      enterHellbound = 0
      try : con.close()
      except : pass
  else :
    enterHellbound = 0
  return int(enterHellbound)

def updateEnter(player) :
  con = L2DatabaseFactory.getInstance().getConnection()
  offline = con.prepareStatement("UPDATE characters SET enterHellbound = ? WHERE charId = ?")
  offline.setInt(1, 1)
  offline.setInt(2, player.getObjectId())
  try :
    offline.executeUpdate()
    offline.close()
    con.close()
  except :
    try : con.close()
    except : pass

def spawnNpc(npcId, x, y, z, h) :
  template = NpcTable.getInstance().getTemplate(npcId)
  spawn = L2Spawn(template)
  spawn.setLocx(x)
  spawn.setLocy(y)
  spawn.setLocz(z)
  spawn.setAmount(1)
  spawn.setHeading(h)
  spawn.setRespawnDelay(60)
  SpawnTable.getInstance().addNewSpawn(spawn, False)
  spawn.init()
  return

def unspawnNpc(npcId) :
  for spawn in SpawnTable.getInstance().getSpawnTable().values():
    if spawn.NpcId() == npcId :
      SpawnTable.getInstance().deleteSpawn(spawn, False)
      npc = spawn.getLastSpawn()
      npc.deleteMe()
  return

class Quest(JQuest) :

 def __init__(self, id, name, descr):
   JQuest.__init__(self, id, name, descr)
   self.checkEnter = checkEnter()
   if self.checkEnter == 0 :
     spawnNpc(32314, 112053, 219594, -3674, 24575)
     spawnNpc(32314, -16899, 209823, -3641, 52156)
   else :
     spawnNpc(32319, 112053, 219594, -3674, 24575)
     spawnNpc(32319, -16899, 209823, -3641, 52156)

 def onTalk(self, npc, player) :
   st = player.getQuestState(qn)
   if not st: return

   npcId = npc.getNpcId()
   st1 = st.getPlayer().getQuestState("133_ThatsBloodyHot")
   st2 = st.getPlayer().getQuestState("130_PathToHellbound")
   if st1 :
     if npcId in WARPGATES and st1.getState() == State.COMPLETED and player.getLevel() >= 78 :
       updateEnter(player)
       self.checkEnter = 1
       player.teleToLocation(-11272, 236464, -3248)
       htmltext = ""
       if npcId == 32314 :
         spawnNpc(32319, 112053, 219594, -3674, 24575)
         spawnNpc(32319, -16899, 209823, -3641, 52156)
     else :
       htmltext = "cant-port.htm"
   elif st2 :
     if npcId in WARPGATES and st2.getState() == State.COMPLETED and self.checkEnter == 1 and player.getLevel() >= 78 :
       updateEnter(player)
       player.teleToLocation(-11272, 236464, -3248)
       htmltext = ""
     else :
       htmltext = "cant-port.htm"
   else :
     htmltext = "cant-port.htm"
   st.exitQuest(1)
   return htmltext
 def onFirstTalk(self, npc, player) :
   htmltext = None
   st1 = player.getQuestState("133_ThatsBloodyHot")
   st2 = player.getQuestState("130_PathToHellbound")
   if st1 :
     npc.showChatWindow(player)
   elif st2 :
     if self.checkEnter == 1 :
       npc.showChatWindow(player)
     else :
       htmltext = "warpgate-locked.htm"
   elif self.checkEnter == 0 :
     htmltext = "warpgate-locked.htm"
   elif self.checkEnter == 1 :
     npc.showChatWindow(player)
   return htmltext

QUEST       = Quest(1107,qn,"Teleports")

for npcId in WARPGATES :
  QUEST.addStartNpc(npcId)
  QUEST.addTalkId(npcId)
  QUEST.addFirstTalkId(npcId)